<template>
  <el-dialog
    :model-value="dialogFormVisible"
    :destroy-on-close="true"
    title="币种信息"
    width="450px"
    @close="emits('closeDialog')"
  >
    <el-form
      ref="dialogFormRef"
      :model="dialogForm"
      :rules="rules"
      :label-width="formLabelWidth"
    >
      <el-form-item label="币种名称" prop="coin_name">
        <el-input v-model="dialogForm.coin_name" />
      </el-form-item>
      <el-form-item label="中文名称" prop="cn_name">
        <el-input v-model="dialogForm.cn_name" />
      </el-form-item>
      <el-form-item label="英文名称" prop="en_name">
        <el-input v-model="dialogForm.en_name" />
      </el-form-item>
      <el-form-item label="账户精度" prop="precision">
        <el-input-number v-model="dialogForm.precision" />
      </el-form-item>
      <div class="flx-center">
        <el-form-item label="是否启用">
          <el-switch v-model="dialogForm.is_active" />
        </el-form-item>
        <el-form-item label="是否默认">
          <el-switch v-model="dialogForm.is_default" />
        </el-form-item>
      </div>
      <div class="flx-center">
        <el-form-item label="是否支持划转">
          <el-switch v-model="dialogForm.is_transfer" />
        </el-form-item>
        <el-form-item label="是否支持OTC">
          <el-switch v-model="dialogForm.is_otc" />
        </el-form-item>
      </div>

      <el-form-item label="排序数字" prop="priority">
        <el-input v-model="dialogForm.priority" />
      </el-form-item>
      <el-form-item label="币种图标">
        <el-upload
          :action="uploadimgUrl"
          :data="{ project_code: 'gmeta' }"
          :show-file-list="false"
          :on-success="handleUploaderImgSuccess"
          :limit="1"
          :headers="TOKENT_HEADER"
          class="avatar-uploader"
        >
          <img v-if="dialogForm.icon" :src="dialogForm.icon_show" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="id" style="display: none">
        <el-input v-model="dialogForm.coin_id" disabled />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="emits('cancel')">取消</el-button>
        <el-button
          type="primary"
          @click="submit(dialogForm)"
          :loading="btnLoading"
        >
          提交
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref } from "vue";
import { uploadimgUrl } from "@/api/system/uploadimg";
import { TOKENT_HEADER } from "@/config/api-constant/system";
const dialogFormRef = ref(null);

defineProps({
  dialogFormVisible: {
    type: Boolean,
    default: false,
  },
  dialogForm: {
    type: Object,
    default: {
      coin_name: "",
      coin_id: "",
      cn_name: "",
      en_name: "",
      icon: "",
      precision: 8,
      is_active: false,
      is_default: false,
      is_transfer: false,
      is_otc: false,
      priority: "",
    },
  },
  btnLoading: {
    type: Boolean,
    default: false,
  },
});
const emits = defineEmits([
  "closeDialog",
  "cancel",
  "submitForm",
  "handleUploaderImgRequest",
]);

const formLabelWidth = "120px";
// 校验
const rules = {
  coin_name: [{ required: true, message: "请输入币种名称", trigger: "blur" }],
  cn_name: [{ required: true, message: "请输入中文名称", trigger: "blur" }],
  en_name: [{ required: true, message: "请输入英文名称", trigger: "blur" }],
  precision: [{ required: true, message: "请输入账户精度", trigger: "blur" }],
  priority: [{ required: true, message: "请输入排序数字", trigger: "blur" }],
};
const handleUploaderImgSuccess = (res) => {
  emits("handleUploaderImgRequest", res);
};
// 提交
const submit = (dialogForm) => {
  dialogFormRef.value
    .validate()
    .then(async () => {
      try {
        const param = Object.assign({}, dialogForm);
        param.is_active = param.is_active ? 1 : 0;
        param.is_default = param.is_default ? 1 : 0;
        param.is_transfer = param.is_transfer ? 1 : 0;
        param.is_otc = param.is_otc ? 1 : 0;
        emits("submitForm", param);
      } catch (error) {
        console.log(error);
      }
    })
    .catch((error) => {
      console.log(error);
    });
};
</script>
